<template>
  <Portlet title="提货信息">
    <template slot="tools">
      <template v-if="updatePick">
        <DialogEditPickup @refresh="refresh" :visible.sync="visible"
                          :detail="detail"></DialogEditPickup>
        <SelectAddressDialog @handleDeliveryAddress="handleDeliveryAddress"
                             :visible.sync="visibleSelect"></SelectAddressDialog>
        <el-button @click.prevent="visibleSelect = true" type="primary">选择提货地址</el-button>
        <el-button @click.prevent="visible = true" type="primary">修改提货信息</el-button>
      </template>
    </template>
    <el-col :lg="8" :md="12" :xl="6">
      <el-form-item label="是否提货：">
        <span>{{ doArriveText }}</span>
      </el-form-item>
    </el-col>
    <el-col :lg="8" :md="12" :xl="6">
      <el-form-item label="预计提货：">
        <span>{{ chOrder.predictDevliveryDate | unixTimestampFilter }}</span>
      </el-form-item>
    </el-col>
    <el-col :lg="8" :md="12" :xl="6">
      <el-form-item label="提货联系人：">
        <span>{{ chOrder.pickUpName }}</span>
      </el-form-item>
    </el-col>
    <el-col :lg="8" :md="12" :xl="6">
      <el-form-item label="提货联系电话：">
        <span>{{ chOrder.pickUpPhone }}</span>
      </el-form-item>
    </el-col>
    <el-col :lg="8" :md="12" :xl="6">
      <el-form-item label="提货备注：">
        <span>{{ chOrder.pickUpMemo }}</span>
      </el-form-item>
    </el-col>
    <el-col :lg="8" :md="12" :xl="6">
      <el-form-item label="提货地址：">
        <span>{{chOrder.pickUpProvince}} {{chOrder.pickUpCity}} {{chOrder.pickUpDistrict}} {{chOrder.pickUpAddress}}</span>
      </el-form-item>
    </el-col>
  </Portlet>
</template>
<script>
import {dotData, confirm} from '@/utils'
import DialogEditPickup from '@/views/oms/orders/serviceOrders/detail/DialogEditPickup'
import SelectAddressDialog from '@/views/oms/orders/serviceOrders/detail/SelectAddressDialog'
export default {
  components: {
    DialogEditPickup,
    SelectAddressDialog
  },
  props: {
    detail: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      isEdit: false,
      visible: false,
      visibleSelect: false
    }
  },
  computed: {
    chOrder() {
      return dotData(this.detail, 'chOrder') || {}
    },
    // 是否能修改提货地址
    updatePick() {
      return parseInt(dotData(this.detail, 'updatePick')) === 1
    },
    id() {
      return dotData(this.chOrder, 'id')
    },
    doArriveText() {
      // arriveState: 1： 部分到货 2: 已到货 0： 未到货
      const doArrive = dotData(this.chOrder, 'doArrive')
      switch (parseInt(doArrive)) {
        case 1:
          return '是'
        case 0:
          return '否'
        default:
          return doArrive
      }
    }
  },
  methods: {
    refresh(val) {
      this.$emit('refresh', val)
    },
    // 调用提货地址的修改接口，修改提货地址
    handleDeliveryAddress(data) {
      console.log(data, 'handleDeliveryAddress')
      let submitData = {
        orderId: this.id,
        pickUpName: dotData(data, 'deliveryName'),
        pickUpPhone: dotData(data, 'deliveryPhone'),
        pickUpAddress: dotData(data, 'deliveryAddress'),
        pickUpProvinceNo: dotData(data, 'deliveryProvinceNo'),
        pickUpCityNo: dotData(data, 'deliveryCityNo'),
        pickUpDistrictNo: dotData(data, 'deliveryDistrictNo')
      }
      this.$ajax.post('/ms-terminal-order/user-pick-up/modify-pick-up', submitData).then(response => {
        const success = dotData(response, 'success')
        if (success) {
          confirm('修改成功', '提示', {
            type: 'success',
            confirmButtonText: '确定',
            showCancelButton: false
          }).finally(_ => {
            this.$emit('refresh', true)
          })
        }
      })
    }
  }
}
</script>
